﻿#include "AppCast.h"

#include <QString>
#include <QDebug>

#include <QTextCodec>

QString ErrMsg;
QString& getErroMsg(int errorCode)
{

    switch (errorCode) {
    case COMM_OK:
        ErrMsg = "通讯正常";
        break;
    case ERR_NO_MEMORY :
        ErrMsg = "内存不足";
        break;
    case ERR_CREATE_SOCKET:
        ErrMsg = "创建套接字失败";
        break;
    case ERR_BIND:
        ErrMsg = "绑定失败";
        break;
    case ERR_JOIN_MUTICAST_GROUP:
        ErrMsg = "加入广播组失败";
        break;
    case ERR_CONNECT:
        ErrMsg = "连接失败";
        break;
    case ERR_SEND:
        ErrMsg = "发送失败";
        break;
    case ERR_RECV:
        ErrMsg = "接收失败";
        break;
    case ERR_DISCONNECT:
        ErrMsg = "断开连接失败";
        break;
    default:
        ErrMsg = "其他错误";
        break;

    }
    return ErrMsg;
}

//QString convertUtf8ToGbk(const QString &utf8String) {
//    QTextCodec *codec = QTextCodec::codecForName("GBK");
//    QString gbkString = codec->toUnicode(utf8String.toUtf8());
//    return gbkString;
//}

//QString convertGBKtoUTF8(const QString &gbkString) {
//    QTextCodec *codec = QTextCodec::codecForName("GBK");
//    QTextCodec *utf8Codec = QTextCodec::codecForName("UTF-8");
//    QString utf8String = utf8Codec->toUnicode(codec->fromUnicode(gbkString));
//    return utf8String;
//}

QString HexToAscII(const QString &hexStr) {
    QByteArray hexData = QByteArray::fromHex(hexStr.toLocal8Bit());
    QString asciiStr = QString::fromLocal8Bit(hexData);
    return asciiStr;
}

QString HexToAscii(QString text)
{
    /* 将Hex字符串转换为Ascii字符串 */
    QTextCodec *tc = QTextCodec::codecForName("System");
    QString temp;
    QByteArray ascii_data;
    QStringList temp_list = text.split(' ', QString::SkipEmptyParts);
    foreach(QString str, temp_list)
    {
        ascii_data.append(str);
    }
    // 将ascii_data中的16进制数据转化为对应的字符串，比如\x31转换为"1"
    temp = tc->toUnicode(QByteArray::fromHex(ascii_data));
    return temp;
}

QString AsciiToHex(QString text)
{
    /* 将Ascii字符串转换为Hex字符串 */
    char byteBuff;
    QString temp;
    QByteArray data = text.toLocal8Bit();
    foreach(char byte, data)
    {
        // 高字节
        byteBuff = (byte & 0xF0)>>4;
        temp += (byteBuff < 10) ? byteBuff+'0' : byteBuff+'A'-10;

        // 低字节
        byteBuff = byte&0x0F;
        temp += (byteBuff < 10) ? byteBuff+'0' : byteBuff+'A'-10;
        temp += ' ';
    }
    return temp;

}

//QString &ErrorCode::getErrorMessage(ERRORCODE errno)
//{
//    switch (errno) {
//    case COMM_OK:
//        ErrorMessage = "通讯正常";
//        break;
//    case ERR_NO_MEMORY :
//        ErrorMessage = "内存不足";
//        break;
//    case ERR_CREATE_SOCKET:
//        ErrorMessage = "创建套接字失败";
//        break;
//    case ERR_BIND:
//        ErrorMessage = "绑定失败";
//        break;
//    case ERR_JOIN_MUTICAST_GROUP:
//        ErrorMessage = "加入广播组失败";
//        break;
//    case ERR_CONNECT:
//        ErrorMessage = "连接失败";
//        break;
//    case ERR_SEND:
//        ErrorMessage= "发送失败";
//        break;
//    case ERR_RECV:
//        ErrorMessage = "接收失败";
//        break;
//    case ERR_DISCONNECT:
//        ErrorMessage = "断开连接失败";
//        break;
//    default:
//        ErrorMessage = "其他错误";
//        break;

//    }
//    qDebug() << "ErrorMessage=" << ErrorMessage <<__FUNCTION__<<__LINE__;
//    return ErrorMessage;
//}
